<!--
 * @Author: your name
 * @Date: 2019-11-04 18:07:31
 * @LastEditTime: 2019-11-04 20:23:29
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \1104作业\likou.html
 -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
	 
		var myAtoi = function(str) {
			str = str.trim();
			let zhengshu = Number.parseInt(str);
			if(Number.isNaN(zhengshu)){
				return 0;
			}
			if(zhengshu < -2147483648){
				return -2147483648
			}
			if(zhengshu > Math.pow(2,31)-1){
				return  Math.pow(2,31)-1;
			}
			
			return zhengshu;
		};
		myAtoi("words and 987");
        var myAtoi = function(str) {
            str = str.trim();
            let arr = "";
            let flag = 0;
            if (str[0] == "-") {
                flag = 1;
                str = str.slice(1);
            } else if (str[0] == "+") {
                str = str.slice(1);
            }
            for (let i = 0; i < str.length; i++) {
                if (str[i].charCodeAt() >= 48 && str[i].charCodeAt() <= 57) {
                    arr += str[i];
                } else {
                    break;
                }
            }
            arr = flag > 0 ? -arr : arr;
            arr = arr != 0 ? arr : 0;
            arr = arr < Math.pow(2, 31) - 1 ? arr : Math.pow(2, 31) - 1;
            arr = arr > Math.pow(-2, 31) - 1 ? arr : Math.pow(-2, 31);
            return arr;
        };

        console.log(myAtoi("193asd"));




        var threeSumClosest = function(nums, target) {
            if (nums.length < 3) {
                return;
            }
            let res = nums[0],
                minDiff = Math.pow(2, 31) - 1;
            for (let i = 0; i < nums.length - 2; i++) {
                for (let j = i + 1; j < nums.length - 1; j++) {
                    for (let k = j + 1; k < nums.length; k++) {
                        let sum = nums[i] + nums[j] + nums[k];
                        let dValue = target - sum;
                        dValue = Math.abs(dValue);
                        if (dValue == 0) {
                            return target;
                        }
                        if (dValue < minDiff) {
                            minDiff = dValue;
                            res = sum;
                        }
                    }
                }

            }
            return res;
        };

        console.log(threeSumClosest([2, 3, 4, 10, -6, 6], 20))
    </script>
</body>

</html>